package Gui;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;

import DB.UserConnector;

@SuppressWarnings("serial")
public class SignInFrame extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	private JTextField textField_3;

	/**
	 * Create the frame.
	 */
	public SignInFrame() {
		setTitle("Sign In");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 310, 180);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		textField = new JTextField();
		textField.setBounds(113, 21, 154, 20);
		contentPane.add(textField);
		textField.setColumns(10);

		textField_3 = new JTextField();
		textField_3.setFont(new Font("Symbol", Font.PLAIN, 11));
		textField_3.setColumns(10);
		textField_3.setBounds(113, 52, 154, 20);
		contentPane.add(textField_3);

		JTextPane txtpnUsername = new JTextPane();
		txtpnUsername.setEditable(false);
		txtpnUsername.setFont(new Font("Tahoma", Font.BOLD, 15));
		txtpnUsername.setBackground(UIManager.getColor("Button.background"));
		txtpnUsername.setText("Name");
		txtpnUsername.setBounds(20, 21, 83, 20);
		contentPane.add(txtpnUsername);

		JTextPane txtpnPassword = new JTextPane();
		txtpnPassword.setEditable(false);
		txtpnPassword.setFont(new Font("Tahoma", Font.BOLD, 15));
		txtpnPassword.setBackground(UIManager.getColor("Button.background"));
		txtpnPassword.setText("Password");
		txtpnPassword.setBounds(20, 52, 83, 20);
		contentPane.add(txtpnPassword);

		JButton btnSignIn = new JButton("Sign In");
		btnSignIn.setFont(new Font("Tahoma", Font.PLAIN, 13));
		btnSignIn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				boolean valid = true;
				String name = textField.getText();
				String pass = textField_3.getText();
				if (name.equals("")) {
					JOptionPane.showMessageDialog(null,"Please enter Username");
					valid = false;
				}else
					if ((pass.equals(""))) {
						JOptionPane.showMessageDialog(null,"Please enter your password");
						valid = false;
					}
					else {
						int res=UserConnector.getInstance().signIn(name, pass);
						if (res==1){ //wrong name or password
							JOptionPane.showMessageDialog(null,"Wrong name/password!");
							valid=false;
						}
						else if (res==-1){ //error connecting to db
							JOptionPane.showMessageDialog(null,"Error connecting to DB");
							valid=false;
						}
						else if (res==0){ //name+pw correct
							valid=true;
						}
					}
				if (valid){
					if (!App.isPlaying(name)){
						App.webgf.addUserToTable(name);
						App.webgf.setVisible(true);
						App.addPlayer(name);
						setVisible(false);
					}
					else {
						JOptionPane.showMessageDialog(null,name+" is already playing!");
					}
				}}
		}
		);
		btnSignIn.setBounds(169, 90, 98, 46);
		contentPane.add(btnSignIn);
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				setVisible(false);
				if (App.getNumOfPlayers()==0){
					addPlayer addplayer = new addPlayer();
					addplayer.setVisible(true);
				}
				else {
					App.webgf.setVisible(true);
				}
			}
		});
		btnCancel.setFont(new Font("Tahoma", Font.PLAIN, 13));
		btnCancel.setBounds(20, 90, 91, 46);
		contentPane.add(btnCancel);
	}
}
